package Thread;

public class Dom25 {
    public static void main(String[] args) throws InterruptedException {
        Object locer1=new Object();
        Object locer2=new Object();
        Object locer3=new Object();
        Thread t1=new Thread(()->{
           try{
               for (int i = 0; i < 10; i++) {
                   synchronized (locer1){
                       locer1.wait();
                   }
                   System.out.print("A");
                   synchronized (locer2){
                       locer2.notify();
                   }
               }
           }catch (InterruptedException e){
               throw new RuntimeException();
           }
        });
        Thread t2=new Thread(()->{
            try{
                for (int i = 0; i < 10; i++) {
                    synchronized (locer2){
                        locer2.wait();
                    }
                    System.out.print("B");
                    synchronized (locer3){
                        locer3.notify();
                    }
                }
            }catch (InterruptedException e){
                throw new RuntimeException();
            }
        });
        Thread t3=new Thread(()->{
            try{
                for (int i = 0; i < 10; i++) {
                    synchronized (locer3){
                        locer3.wait();
                    }
                    System.out.println("C");
                    synchronized (locer1){
                        locer1.notify();
                    }
                }
            }catch (InterruptedException e){
                throw new RuntimeException();
            }
        });
        t1.start();
        t2.start();
        t3.start();
        //主线程中通知t1,让上述逻辑从t1开始执行
        //需要保证上述过程都wait,再进行notify
        Thread.sleep(1000);
        synchronized (locer1){
            locer1.notify();
        }
    }
}
